// pages/articel-detail/articel-detail.js
const util = require('../../utils/util.js')
let WxParse = require('../../wxParse/wxParse.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    newList: {},
    detail:{},
    id: '',
    param: {
      'type': 1,
      'page': 1,
      'size': 6
    },
    is_login: true,  //是否登录  ,true登录
    login_text: '请登录',
    is_fabulous:false, 
    is_show:false,
    chatimestamp:0,
    from_uid:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中...',
    })
    let id = options.id != undefined ? options.id : this.data.id;
    let from_uid = options.from_uid != undefined ? options.from_uid : this.data.from_uid;
    if(id!=undefined){
      this.setData({
        id:id,
      })
    }
    if (from_uid != undefined) {
      this.setData({
        from_uid: from_uid,
      })
    }
    this.data.id = id;
    let wxarticle = app.url.wxarticle;
    util.http(wxarticle, this.newResponse, this.data.param)
    var detail = app.url.detail + '?id=' + id;
    util.http(detail, this.detailResponse)
  },
  //登录
  login: function (userinfo) { // 需要一个参数来额外接收用户数据
    this.setData({
      is_login: app.globalData.is_login
    })
    if (userinfo.detail.errMsg == 'getUserInfo:ok') {
      this.setData({
        login_text: '正在登录中...'
      })
    }
    var from_uid=this.data.from_uid;
    console.log(from_uid,'分享id')
    util.login((res) => {
      console.log(res, '回调')
      if (res.errMsg == 'request:ok') {
        app.globalData.is_login = true
        this.setData({
          is_login: true
        })
        this.onLoad(this);
        //当处理点赞时
        if (this.data.is_fabulous==true){
          this.onLike()
        }

      }
      if (userinfo.detail.errMsg == 'getUserInfo:fail auth deny') {
        //登录授权拒绝
        this.setData({
          is_login: true
        })
      }
    }, from_uid)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var url = app.url.wxarticle;
    util.http(url, this.newResponse, this.data.param)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  onHotArticle: function () {
    console.log(111)
    wx.reLaunch({
      url: '../index/index',
    })
  },

  newResponse: function (res) {
    let newList = res.data.list
    let f_newList = {};
    console.log(!newList)
    if(!newList){
      this.onReachBottom();
    }else{
      if (this.data.newList.length > 0) {
        f_newList = this.data.newList.concat(newList)
      } else {
        f_newList = newList;
      }
      this.data.param.page +=1;
      // this.data.param.type = Math.floor(Math.random() * 10 + 1) 
      this.setData({
        newList: f_newList,
        is_show: true,
      })
    }
  },

  onLike: function () {
    //登录
    if (app.globalData.is_login==false){
      this.setData({
        is_login: app.globalData.is_login,
        is_fabulous:true,
      }) 
      return false
    }
    var backstage_url= app.url.backstage+"user/fabulous";
    var a_id=this.data.id;
    var user_id = wx.getStorageSync('uid');
    var param={
      'a_id': a_id,
      'user_id': user_id,
      'type':1,      //type 1为文章(写死)
      'title': this.data.detail.title,
      'source': this.data.detail.source,
    }
    util.http(backstage_url, this.addBackstage, param)
  },
  //点赞保存数据返回结果
  addBackstage:function(res){
    console.log(res)
    if(res.data.errorCode=='00000'){
      wx.showToast({
        title: '赞+1',
        icon: 'succes',
        duration: 2000,
        image:'../../images/icon/like.png',
        mask: true
      })
    }
    if(res.data.errorCode=='10003'){
      wx.showToast({
        title: res.data.errorMsg,
        icon: 'none',
        duration: 2000,
      })
    }
  },
  
  onDetail: function (event) {
    let id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../articel-detail/articel-detail?id=' + id,
    })
  },
  onShareAppMessage: function (res) {

    if (res.from === 'button') {
      // 来自页面内转发按钮"pages/articel-detail/articel-detail"
      console.log(res.target)
    }
    if (app.globalData.is_login == true){
      var from_uid = wx.getStorageSync('uid');
    }else{
      var from_uid = '';
    }
    return {
      title: this.data.detail.title,
      path: '/pages/articel-detail/articel-detail?id=' + this.data.id +'&from_uid='+from_uid,
      imageUrl: this.data.detail.images[0]
    }
  },
  detailResponse:function(res){
    var strtime = res.data.publish_time;
    //文章时间戳
  //   var timestamp2 = Date.parse(new Date(strtime));
  //   timestamp2 = timestamp2 / 1000;
  //   console.log(strtime + "的时间戳为：" + timestamp2);
  //  //当前时间戳
  //   var nowtimestamp = Date.parse(new Date());
  //   nowtimestamp = nowtimestamp / 1000;
  //   var chatimestamp = parseInt((parseInt(nowtimestamp) - parseInt(timestamp2))/60)
  //   console.log(chatimestamp,'时间差')
  //   if(chatimestamp>3){
  //     chatimestamp=3;
  //   }
   WxParse.wxParse('content', 'html', res.data.content, this, 5);
  
   setTimeout(
     function () {
       wx.hideLoading();
     },
     500
   )
    this.setData({
      detail: res.data,
      chatimestamp: strtime
    })
  }
})